javascript:(function(){
	if (typeof FB == 'undefined') {
		alert('Error1: FB session not active, try reloading the game and unframing again.');
		return;	
	}

	var friends = [];
	var version = 'List FB Friends v1.01';
	try {
		var fbid = FB.Facebook.apiClient.get_session().uid;
	}
	catch (fbiderr) {
		alert('Error2: FB session not active, try reloading the game and unframing again.');
	}
	var fql1="SELECT uid,name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1="+fbid+") AND is_app_user = 1 ORDER BY name";
	var fql2="SELECT uid,name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1="+fbid+") AND is_app_user = 0 ORDER BY name";
	var fql3="SELECT uid,name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1="+fbid+") ORDER BY name";
	content=document.getElementById('popup_fodder');

	var config_html = 
	'<style type="text/css">'+
		'.messages img{margin:0 3px;vertical-align:top}'+
		'.messages input {border: 1px solid #FFF;margin 0;padding 0;background: #000; color: #FFF; width: 20px;}'+
		'#play{display:none}'+
		'#pause{display:inline}'+
		'#close{display:inline}'+
	'</style>'+
	'<table class="messages">'+
	'<tr>'+
		'<td colspan="3" align="right" style="text-align:right;font-size:0.8em;">'+version+' - <a href="http://www.spockholm.com/mafia/bookmarklets.php" target="_top">Spockholm Mafia Tools</a> - <a href="http://www.spockholm.com/mafia/donate.php?ListFBFriends" alt="Buy me a beer" target="_blank">Donate</a> - <a href="#" id="close"><img src="http://www.spockholm.com/mafia/stop.gif" title="Close" width="16" height="16"></a></td></tr>'+
	'</tr>'+
	'<tr>'+
		'<td colspan="3">'+
			'Load: <a id="mwfriends" href="#">MW playing</a> - <a id="nonmwfriends" href="#">Not MW playing</a> - <a id="fbfriends" href="#">All FB</a> - friends.'+
		'</td>'+
	'<tr>'+
		'<td>Status:</td>'+
		'<td id="status" align="left" colspan="2"></td>'+
	'</tr>'+
	'<tr>'+
		'<td colspan="3"><textarea name="friendlist" id="friendlist" class="instructions" rows="15" cols="50">'+
			'Enter in this box a list of your mafia.\n'+
		'</textarea></td>'+
	'</tr>'+
	'</table>';
	
	//Pistol Pete FQL code
	function parseFBFr(fql){
		//alert(fql);
		var f=function(){
			var results=[];
			var friendquicklist;
			var friendcount = 0;
			return {
				getResults: function(){ return results; },
				process: function(fbfr){
					if (fbfr){
						for(i=0;i<fbfr.length;i++) {
						  //results[results.length]={	'id':fbfr[i].uid, 'name':fbfr[i].name };
							if (fbfr[i].name != null) {
								friendquicklist += fbfr[i].uid+', '+ fbfr[i].name.replace(/[^A-Za-z0-9 ]/g, '') +'\n';
								friendcount++;
								//friends.pop(fbfr[i].uid+', '+fbfr[i].name.replace(/[^A-Za-z0-9 ]/g, ''));
							}
						}
						//friends=friendarray.split(',');
					}
					else { 
						friendquicklist='Unable to retrive list. Please try again.';
					}
					//friend=results;
					document.getElementById('friendlist').value = friendquicklist.replace(/undefined/,'');
					document.getElementById('status').innerHTML = 'Loaded '+friendcount+' friends';
				}
			};
		}();
		document.getElementById('friendlist').value = 'Loading...Be Patient...';
		FB.Facebook.apiClient.fql_query(fql, f.process);
		return;
	}
	
	function create_div() {
		if(document.getElementById('spockdiv')) {
			document.getElementById('spockdiv').innerHTML = config_html;
		}
		else {
			var spock_div=document.createElement("div");
			spock_div.id = 'spockdiv';
			spock_div.innerHTML = config_html;
			content.insertBefore(spock_div, content.firstChild);
		}
	}
	create_div();
	parseFBFr(fql1);

	document.getElementById('mwfriends').onclick=function(e) {
		document.getElementById('friendlist').focus();
		parseFBFr(fql1);
	};
	document.getElementById('nonmwfriends').onclick=function(e) {
		document.getElementById('friendlist').focus();
		parseFBFr(fql2);
	};
	document.getElementById('fbfriends').onclick=function(e) {
		document.getElementById('friendlist').focus();
		parseFBFr(fql3);
	};
	document.getElementById('close').onclick=function(e) {
		document.getElementById('popup_fodder').removeChild(document.getElementById('spockdiv'));
		return false;
	};
	function dirtypop() {
		var generator=window.open('','name','height=500,width=500');
		generator.document.write('<html><head><title>Your Facebook Friends</title>');
		generator.document.write('</head><body>');
		generator.document.write('<pre>Number of friends in this list: '+friends.length+'</pre><textarea rows="20">');
		for(i = 0; i < friends.length; i++) {
			generator.document.write(friends[i]+'\n');
		}
		generator.document.write('</textarea><pre><a href="javascript:self.close()">Close</a> the popup.</pre>');
		generator.document.write('</body></html>');
		generator.document.close();
	}
	

})()
